//
//  ChangeSceneLayer.cpp
//  PlantNannyDemo
//
//  Created by Joueu on 14-10-31.
//
//
#include "ChangeSceneLayer.h"
#include "DictionaryKeyNames.h"
#include "DataCenter.h"

bool ChangeSceneLayer::init()
{
    auto visibleSize = Director::getInstance() -> getVisibleSize();
    m_colorLayer = LayerColor::create(Color4B(255, 255, 255, 0.6 * 255), visibleSize.width, 227);
    m_colorLayer -> setAnchorPoint(Point (0,0));
    this -> addChild(m_colorLayer);
    
    auto label = Label::createWithSystemFont("选择一个喜欢的场景", "微软雅黑", 25);
    label -> setTextColor(Color4B(0,0,0,0));
    label -> setPosition(Point(visibleSize.width * 0.5, 204));
    
    
    m_colorLayer -> addChild(label);
    m_tableView = TableView::create(this, Size(visibleSize.width, 111 + 6));
    m_tableView -> setDelegate(this);
    m_tableView -> setDirection(extension::ScrollView::Direction::HORIZONTAL);
    m_tableView -> setPosition(Point(0, 50));
    m_tableView -> reloadData();
    m_colorLayer -> addChild(m_tableView);
    return true;
}



ssize_t ChangeSceneLayer::numberOfCellsInTableView(TableView *table)
{
    return DataCenter::getInstance() -> getSceneCount();
}

TableViewCell *ChangeSceneLayer::tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx)
{
    
    Size visibleSize = Director::getInstance() -> getVisibleSize();
    TableViewCell *cell = table -> dequeueCell();
    
    if (!cell) {
        cell = new TableViewCell();
        
        cell -> setAnchorPoint(Point(0,0));
        cell -> setPosition(Point(0,200));
        cell -> autorelease();
        
        Sprite *sprite = Sprite::create(DataCenter::getInstance() -> getMoreSceneByType((int)idx + 1));
        sprite -> setTag(123456);
        sprite -> setPosition(Point(visibleSize.width / 6, 58));
        cell -> addChild(sprite);
    }else{
        Sprite *sprite = (Sprite *)cell -> getChildByTag(123456);
        sprite -> setTexture(DataCenter::getInstance() -> getMoreSceneByType((int)idx + 1));
        auto frame = cell -> getChildByTag(13800);
        if (frame) {
            frame -> removeFromParentAndCleanup(true);
        }
    }
    cell -> setTag((int)(idx + 1));
    return cell;
}

Size ChangeSceneLayer::cellSizeForTable(cocos2d::extension::TableView *table)
{
    Size visibleSize = Director::getInstance() -> getVisibleSize();
    return Size(visibleSize.width / 3, 110);
}

void ChangeSceneLayer::tableCellTouched(TableView* table, TableViewCell* cell)
{
    for (int i = 0; i < this -> numberOfCellsInTableView(table); i++) {
        auto celll = (TableViewCell *)table -> cellAtIndex(i);
        if (celll != NULL) {
            auto sprite = celll -> getChildByTag(13800);
            if (sprite) {
                sprite -> removeFromParentAndCleanup(true);
            }
        }
    }
    
    Sprite *sprite = (Sprite *)(cell -> getChildByTag(123456));
    int tag = cell -> getTag();
    Sprite *frame = Sprite::create("frame.png");
    frame -> setTag(13800);
    frame -> setPosition(sprite -> getPosition());
    cell -> addChild(frame,-1);
    log("%d", tag);
    
    __Dictionary *dict = __Dictionary::create();
    dict -> setObject(__Integer::create((int)tag), "Type");
    CCNotificationCenter::getInstance() -> postNotification("ChangeTheTheme", dict);

}

void ChangeSceneLayer::tableCellHighlight(TableView* table, TableViewCell* cell)
{
    
}

void ChangeSceneLayer::tableCellUnhighlight(TableView* table, TableViewCell* cell)
{
}
